/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.common.web.exception;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;

/**
 * Exception handler para todas las exceptions de la aplicacion.<br>
 * 
 * @author nicolas.gonzalez
 * @see ExceptionHandler
 */
public class PosgradoExceptionHandler extends ExceptionHandler {

    private static Logger logger = Logger.getLogger(PosgradoExceptionHandler.class);
    
    /**
     * @see org.apache.struts.action.ExceptionHandler#execute(java.lang.Exception, org.apache.struts.config.ExceptionConfig, org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward execute(Exception ex, ExceptionConfig ae,
            ActionMapping mapping, ActionForm formInstance,
            HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        MessageResources bundle = MessageResourcesFactory.createFactory().createResources("ApplicationResources");
        logger.error("Exception handler received exception: ", ex);

        String descriptionError = bundle.getMessage(ae.getKey());
        request.setAttribute("description", descriptionError);
        
        String exceptionMessage = ex.getMessage();
        String exceptionStackTrace = ExceptionUtils.getFullStackTrace(ex);

        request.setAttribute("exceptionMessage", exceptionMessage);
        request.setAttribute("exceptionStackTrace", exceptionStackTrace);
        
        return mapping.findForward("failure");
    }
    
}
